<?php
require dirname(dirname(__FILE__)) . '/include/conf.php';
require dirname(dirname(__FILE__)) . '/include/basefunc.php';

// 读取抓数据开关
$errmsg_set = "Open config file error\n";
$set_str = conf_switch_sets('get');
if ($set_str == 'start'){
	unset($errmsg_set);
}elseif ($set_str == 'stop'){
	$errmsg_set = "Closed switch\n";
}
if (isset($errmsg_set)){
	print_log($errmsg_set);
}

$set_vals = conf_pair_sets('get');
if ($set_vals !== false){
	$profit_rate = $set_vals['profit_rate'];
	$money_188 = $set_vals['money_188'];
	$same_times = $set_vals['same_times'];
	$reminder = $set_vals['reminder'];
}else {
	// 未设置过配水参数或读取配置失败，使用如下默认值
	$profit_rate = 0;
	$money_188 = 1000;
	$same_times = 10;
	$reminder = 1;
}

// 抓取时间
if (!isset($argv[1]) || !is_numeric($argv[1])){
	print_log('Parameter error!');
}
$nowtimestamp = (int) $argv[1];
$nowtime = date('Y-m-d H:i:s', $nowtimestamp);

$sql = "select * from ball_disc where fetch_time='{$nowtime}' and home_team_id>0 and visiting_team_id>0 and home_team!='' and visiting_team!=''";

require dirname(dirname(__FILE__)) . '/include/database.php';
$db = db_touzhu();
$re = $db->query($sql);

if (is_array($re) && !empty($re)){
	$match_groups = $pair_succ = $err_group = array();
	
	foreach ($re as $v){
		if (empty($v['pankou'])){
			continue;
		}
		$_pankou = str_replace('.0', '', $v['pankou']);
		$time_suffix = strtotime($v['fetch_time']);
		$key = $v['home_team_id'] . '_' . $v['visiting_team_id'] . '_' . $v['wanfa'] . '_' . $_pankou . '_' . $time_suffix;
		$match_groups[$key][] = $v;
	}
	
	foreach ($match_groups as $match_group){
		if (count($match_group) == 2){
			if ($match_group[0]['from'] == 1){
				$match_365 = $match_group[0];
				$match_188 = $match_group[1];
			}else {
				$match_365 = $match_group[1];
				$match_188 = $match_group[0];
			}
			if ($match_365['over'] > 0 && $match_365['under'] > 0 && $match_188['over'] > 0 && $match_188['under'] > 0){
				$over_365 = $match_365['over'];
				$under_365 = $match_365['under'];
				$over_188 = $match_188['over'] + 1;
				$under_188 = $match_188['under'] + 1;
			}else {
				continue;
			}
			
			// 配水计算 - 365大水 vs 188小水
			$win_188 = $money_188 * $under_188;
			$money_365 = round(($win_188 / $over_365), 2);
			$money_earn = $win_188 - $money_365 - $money_188;
			$profitrate = round($money_earn / ($money_365 + $money_188) * 1000, 2);
			//if ($profitrate >= $profit_rate){
				$pair_succ[] = array(
					'match_time' => $match_188['match_time'],
					'match_name' => $match_188['match_name'],
					'home_team' => $match_188['home_team'],
					'visiting_team' => $match_188['visiting_team'],
					'home_team_id' => $match_188['home_team_id'],
					'visiting_team_id' => $match_188['visiting_team_id'],
					'wanfa' => $match_188['wanfa'],
					'pankou' => $match_188['pankou'],
					'over_365' => $over_365,
					'under_365' => $under_365,
					'over_188' => $over_188,
					'under_188' => $under_188,
					'bet_big' => 1,
					'money_365' => $money_365, // over
					'money_188' => $money_188,
					'money_earn' => round($money_earn, 2),
					'profitrate' => $profitrate,
					'fetch_time' => $match_188['fetch_time']
				);
			//}
			
			// 配水计算 - 365小水 vs 188大水
			$win_188 = $money_188 * $over_188;
			$money_365 = round(($win_188 / $under_365), 2);
			$money_earn = $win_188 - $money_365 - $money_188;
			$profitrate = round($money_earn / ($money_365 + $money_188) * 1000, 2);
			//if ($profitrate >= $profit_rate){
				$pair_succ[] = array(
					'match_time' => $match_188['match_time'],
					'match_name' => $match_188['match_name'],
					'home_team' => $match_188['home_team'],
					'visiting_team' => $match_188['visiting_team'],
					'home_team_id' => $match_188['home_team_id'],
					'visiting_team_id' => $match_188['visiting_team_id'],
					'wanfa' => $match_188['wanfa'],
					'pankou' => $match_188['pankou'],
					'over_365' => $over_365,
					'under_365' => $under_365,
					'over_188' => $over_188,
					'under_188' => $under_188,
					'bet_big' => 2,
					'money_365' => $money_365, // under
					'money_188' => $money_188,
					'money_earn' => round($money_earn, 2),
					'profitrate' => $profitrate,
					'fetch_time' => $match_188['fetch_time']
				);
			//}
			
		}else {
			if (count($match_group) > 2){
				$_err_item = array();
				foreach ($match_group as $err_item){
					$_err_item[] = $err_item['id'];
				}
				$err_group[] = 'Id:' . implode(',', $_err_item);
			}
		}
	}
	
	if (!empty($err_group)){
		echo date('Y-m-d H:i:s') . "\t有问题的数据:", implode('||', $err_group), "\n";
	}

	if (!empty($pair_succ)){
		$num_succ = 0;
		$arr_fail = array();
		$in_the_match_time = date('Y-m-d H:i:s', time() - 10 * 60 * 60);
		$sql_fields = 'insert into match_paired (match_time, match_name, home_team, visiting_team, home_team_id,';
		$sql_fields .= 'visiting_team_id, wanfa, pankou, over_365, under_365, over_188, under_188, money_365,';
		$sql_fields .= 'money_188, bet_big, money_earn, profit_rate, fetch_time, add_time) values';
		foreach ($pair_succ as $match){
			/*/ 检查出现相同盘口最多出现次数
			$sql_check = "select count(id) as total from match_paired where home_team_id={$match['home_team_id']}";
			$sql_check .= " and visiting_team_id={$match['visiting_team_id']} and fetch_time>='{$in_the_match_time}'";
			$re_check = $db->query($sql_check);
			$num_check = $re_check[0]['total'];
			if ($num_check >= $same_times){
				continue;
			}*/
			
			$vals = array(
				'match_time' => $match['match_time'],
				'match_name' => $match['match_name'],
				'home_team' => $match['home_team'],
				'visiting_team' => $match['visiting_team'],
				'home_team_id' => $match['home_team_id'],
				'visiting_team_id' => $match['visiting_team_id'],
				'wanfa' => $match['wanfa'],
				'pankou' => $match['pankou'],
				'over_365' => $match['over_365'],
				'under_365' => $match['under_365'],
				'over_188' => $match['over_188'],
				'under_188' => $match['under_188'],
				'money_365' => $match['money_365'],
				'money_188' => $match['money_188'],
				'bet_big' => $match['bet_big'],
				'money_earn' => $match['money_earn'],
				'profit_rate' => $match['profitrate'],
				'fetch_time' => $match['fetch_time'],
				'add_time' => date('Y-m-d H:i:s')
			);
			$sql_add = $sql_fields . "('" . implode("','", $vals) . "')";
			$re_add = $db->query($sql_add);
			if ($re_add){
				$num_succ ++;
			}else {
				$arr_fail[] = $sql_add;
			}
		}
		echo date('Y-m-d H:i:s') . "\t成功写入{$num_succ}条\n";
		if (!empty($arr_fail)){
			echo date('Y-m-d H:i:s') . "\tfail record:" . implode('||', $arr_fail) . "\n";
		}
	}else {
		print_log('没有打到水');
	}

}else {
	print_log('该时间点没有抓到数据');
}
